package com.spring.sperapc.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.spera.genutil.TokenUtil;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.User;
import com.spring.sperapc.web.dao.FamilyMapper;
import com.spring.sperapc.web.dao.FamilyMemberMapper;
import com.spring.sperapc.web.model.Baby;
import com.spring.sperapc.web.model.Family;
import com.spring.sperapc.web.model.FamilyMember;
import com.spring.sperapc.web.service.UserFamilyService;
//import com.spring.tools.MyTools;

/**
 * Created by wojunyu on 2015/8/12.
 */
@Service
public class UserFamilyServiceImpl implements UserFamilyService{
    @Resource
    private FamilyMapper familyMapper;

    @Resource
    private FamilyMemberMapper familyMemberMapper;
    
	@Resource
	private JedisCache jedisCache;

    @Override
    public int deleteByPrimaryKey(@Param(value = "id") String id) {
        return familyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 用户新增家庭，并将自己添加到家庭成员中去
     * @param record
     * @param u
     * @return
     */
    @Override
    public int insertSelective(Family record,User u) {
        int ret = 0;
        int f = familyMapper.insertSelective(record);
        FamilyMember fmModel = new FamilyMember();
        fmModel.setcFamilyId(ApplicationUtils.randomUUID());
        fmModel.setcFamilyId(record.getId());
        fmModel.setcRoleId("1");
        int fm = familyMemberMapper.insertSelective(fmModel);
        if(f==1&&fm==1){
            ret = 1;
        }
        return ret;
    }

    /**
     * 按条件查询家庭信息
     * @param m
     * @return
     */
    @Override
    public Family selectByPrimaryKey(Map m) {
        return familyMapper.selectByPrimaryKey(m);
    }

    /**
     * 修改家庭信息
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Family record) {
        return familyMapper.updateByPrimaryKeySelective(record);
    }
    
    
    
    
    @Override
	public Map<String, Object> getBabyByParent(String token) throws Exception {
		Map<String, Object> m = Maps.newHashMap();
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}
		
//		String c_user_id = jedisCache.lindex(token, 0);
		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);

		List<Baby> list = familyMapper.getBabyByParent(c_user_id);
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");
		return m;
	}
    
    @Override
	public Map<String, Object> getBabyByParentId(String c_user_id) throws Exception {
		Map<String, Object> m = new HashMap();
		if (ApplicationUtils.isNotNull(c_user_id)) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "参数不可为空");
			return m;
		}

		List<Baby> list = familyMapper.getBabyByParent(c_user_id);
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");
		return m;
	}
    
    @Override
 	public Map<String, Object> getRoleList() throws Exception {
 		Map<String, Object> m = new HashMap();



 		List<HashMap> list = familyMapper.getRoleList();

 		m.put(ApplicationUtils.DATASET, list);
 		m.put(ApplicationUtils.STATUS, "1");
 		m.put(ApplicationUtils.MSG, "查询成功");

 		return m;
 	}
    
}
